/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef EVENT_WORKER_H
#define EVENT_WORKER_H

#include "../network/worker_thread.h"
#include "../network/network.h"
#include "event_handler.h"
#include "multicaster.h"

#include <boost/shared_ptr.hpp>

namespace handler {
	class multicaster;
}

/** 
 * \brief This namespace includes all general purpose handler-functions
 * \author Michael Opitz
 */
namespace handler {
	/** 
	 * \brief This class calls/dispatches the event_handler-callbacks.
	 */
	class event_worker : public network::worker_thread {
	public:
		/** 
		 * \brief event_worker is the constructor of this class
		 * 
		 * \param m is a multicaster, at which the event_handlers
		 * registers themselves.
		 * \param q is the message_queue, into which the events are
		 * inserted by the receiver and connection_listener classes.
		 */
		event_worker(boost::weak_ptr<handler::multicaster> m, 
				boost::shared_ptr<network::message_queue> q)
		:_dispatcher(m), _queue(q) {}
		virtual ~event_worker();

		/** 
		 * \brief run runs the thread. Don't call this directly, use
		 * start_thread instead.
		 */
		virtual void run();
		/** 
		 * \brief set_multicaster sets a new multicaster
		 * 
		 * \param w is the new multicaster.
		 */
		void set_multicaster(boost::weak_ptr<handler::multicaster> w)
		{
			_dispatcher = w;
		}
	private:
		boost::weak_ptr<handler::multicaster> _dispatcher;
		boost::shared_ptr<network::message_queue> _queue;
	};
}

#endif
